route.ts
基本信息
- 类型: API 路由
- 路径:
./src/app/api/prompts/[id]/comments/route.ts - 路由:
/api/prompts/[id]/comments
概述
Prompt 评论接口。支持获取评论列表和添加新评论,支持嵌套回复。
HTTP 方法
- GET: 获取评论列表
- POST: 创建新评论
路径参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| id | string | 是 | Prompt ID |
GET - 获取评论列表
功能特性
- 返回所有顶级评论和回复
- 管理员可以看到被标记的评论(shadow-ban)
- 显示当前用户的投票状态
- 返回每个评论的回复数量
响应
成功 (200)
{
"comments": [
{
"id": "comment_id",
"content": "评论内容",
"createdAt": "2024-01-01T00:00:00Z",
"updatedAt": "2024-01-01T00:00:00Z",
"parentId": null,
"flagged": false,
"author": {
"id": "user_id",
"name": "用户名",
"username": "username",
"avatar": "...",
"role": "USER"
},
"score": 5,
"userVote": 1,
"replyCount": 3
}
]
}
功能禁用 (403)
{
"error": "feature_disabled",
"message": "Comments are disabled"
}
未找到 (404)
{
"error": "not_found",
"message": "Prompt not found"
}
POST - 创建评论
Body 参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| content | string | 是 | 评论内容,1-10000 字符 |
| parentId | string | 否 | 父评论 ID(回复时使用) |
响应
成功 (200)
{
"comment": {
"id": "comment_id",
"content": "评论内容",
"createdAt": "2024-01-01T00:00:00Z",
"updatedAt": "2024-01-01T00:00:00Z",
"parentId": null,
"flagged": false,
"author": { ... },
"score": 0,
"userVote": 0,
"replyCount": 0
}
}
错误
未授权 (401)
{
"error": "unauthorized",
"message": "You must be logged in"
}
验证失败 (400)
{
"error": "validation_error",
"message": "Invalid input"
}
父评论无效 (400)
{
"error": "invalid_parent",
"message": "Parent comment not found"
}
依赖
next/server- Next.js 服务器组件zod- 输入验证@/lib/auth- 认证@/lib/db- Prisma 数据库客户端@/lib/config- 应用配置
权限
- GET: 公开访问(私有 Prompt 的评论仅作者可访问)
- POST: 需要登录
通知功能
创建评论时会自动触发通知:
- 如果评论 Prompt 不是自己的,通知 Prompt 作者
- 如果是回复评论,同时通知父评论作者(如果不是自己且不是 Prompt 作者)